package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.itheima.entity.StatusCode;
import com.itheima.pojo.Picture;
import com.itheima.service.PictureService;
import com.itheima.utils.ResultUtil;
import com.itheima.vo.PictureVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/picture")
@Api(tags = "图片相关接口")
@CrossOrigin
public class PictureController {

    @Autowired
    private PictureService pictureService;

    /**
     * 添加图片
     *
     * @param pictureVOList
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加图片")
    public String add(@RequestBody(required = false) List<PictureVO> pictureVOList) {
        return pictureService.addPicture(pictureVOList);
    }

    /**
     * 删除图片
     *
     * @param pictureVO
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation("删除图片")
    public String delete(@RequestBody(required = false) PictureVO pictureVO) {
        System.out.println(pictureVO.getUid()+"========"+pictureVO.getStatus());
        return pictureService.deletePicture(pictureVO);
    }

    /**
     * 编辑图片
     *
     * @param pictureVO
     * @return
     */
    @PostMapping("/edit")
    @ApiOperation("编辑图片")
    public String edit(@RequestBody(required = false) PictureVO pictureVO) {
        return pictureService.editPicture(pictureVO);
    }

    /**
     * 获取图片列表
     *
     * @param pictureVO
     * @return
     */
    @PostMapping("/getList")
    @ApiOperation("获取图片列表")
    public String getList(@RequestBody(required = false) PictureVO pictureVO) {
        Page<Picture> pageList = pictureService.getPageList(pictureVO);
        Map<String, Object> map = new HashMap();
        map.put("records",pageList.getResult());
        map.put("total",pageList.getTotal());
        map.put("size",pageList.getPageSize());
        map.put("current",pageList.getPageNum());
        map.put("orders","");
        map.put("optimizeCountSql",true);
        map.put("isSearchCount",true);
        return ResultUtil.result("success", map);
    }

    /**
     * 通过Uid设置为封面
     *
     * @param pictureVO
     * @return
     */
    @PostMapping("/setCover")
    @ApiOperation("通过Uid设置为封面")
    public String setCover(@RequestBody(required = false) PictureVO pictureVO) {
        return pictureService.setPictureCover(pictureVO);
    }
}
